package com.clever.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clever.bean.shopping.projo.output.ConversationDetailVO;
import com.clever.util.SpringUtil;
import com.clever.annotation.Auth;
import com.clever.annotation.AuthGroup;
import com.clever.bean.model.OnlineUser;
import com.clever.bean.model.Result;

import java.util.List;

import com.clever.bean.shopping.Conversation;
import com.clever.service.ConversationService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 会话表接口
 *
 * @Author xixi
 * @Date 2024-04-09 09:09:12
 */
@RestController
@Validated
@RequestMapping("/conversation")
@AuthGroup(value = "order-second-hand-transaction.conversation", name = "会话表模块", description = "会话表模块权限组")
public class ConversationController {

    @Resource
    private ConversationService conversationService;


    /**
     * 分页查询会话表列表
     *
     * @param pageNumber 页码
     * @param pageSize   每页记录数
     * @param senderId   发送者
     * @param receiverId 接收者
     * @param productId  发起会话商品
     * @return 当前页数据
     */
    @GetMapping("/page/{pageNumber}/{pageSize}")
    @Auth(value = "order-second-hand-transaction.conversation.page", name = "会话表分页", description = "会话表分页接口")
    public Result
            <Page<Conversation>> selectPage(@PathVariable("pageNumber") Integer pageNumber, @PathVariable("pageSize") Integer pageSize, String senderId, String receiverId, String productId) {
        return new Result<>(conversationService.selectPage(pageNumber, pageSize, senderId, receiverId, productId), "分页数据查询成功");
    }

    /**
     * 根据发送者获取列表
     *
     * @param senderId 发送者
     * @return List<Conversation> 会话表列表
     */
    @GetMapping("/listBySenderId/{senderId}")
    @Auth(value = "order-second-hand-transaction.conversation.listBySenderId", name = "根据发送者获取会话表列表", description = "根据发送者获取会话表列表接口")
    public Result
            <List<Conversation>> selectListBySenderId(@PathVariable("senderId") String senderId) {
        return new Result<>(conversationService.selectListBySenderId(senderId), "查询成功");
    }

    /**
     * 根据接收者获取列表
     *
     * @param receiverId 接收者
     * @return List<Conversation> 会话表列表
     */
    @GetMapping("/listByReceiverId/{receiverId}")
    @Auth(value = "order-second-hand-transaction.conversation.listByReceiverId", name = "根据接收者获取会话表列表", description = "根据接收者获取会话表列表接口")
    public Result
            <List<Conversation>> selectListByReceiverId(@PathVariable("receiverId") String receiverId) {
        return new Result<>(conversationService.selectListByReceiverId(receiverId), "查询成功");
    }

    /**
     * 根据发起会话商品获取列表
     *
     * @param productId 发起会话商品
     * @return List<Conversation> 会话表列表
     */
    @GetMapping("/listByProductId/{productId}")
    @Auth(value = "order-second-hand-transaction.conversation.listByProductId", name = "根据发起会话商品获取会话表列表", description = "根据发起会话商品获取会话表列表接口")
    public Result
            <List<Conversation>> selectListByProductId(@PathVariable("productId") String productId) {
        return new Result<>(conversationService.selectListByProductId(productId), "查询成功");
    }

    /**
     * 根据id获取会话表信息
     *
     * @param id id
     * @return 会话表信息
     */
    @GetMapping("/{id}")
    @Auth(value = "clever-system.conversation.selectById", name = "根据id获取会话表信息", description = "根据id获取会话表信息接口")
    public Result<Conversation> selectById(@PathVariable("id") String id) {
        return new Result<>(conversationService.selectById(id), "查询成功");
    }

    @GetMapping("/my")
    public Result<List<ConversationDetailVO>> myConversation() {
        return new Result<>(conversationService.myConversation(SpringUtil.getOnlineUser().getId()));
    }

    /**
     * 创建会话表信息
     *
     * @return 创建后的会话表信息
     */
    @PostMapping("")
    @Auth(value = "order-second-hand-transaction.conversation.create", name = "创建会话表", description = "创建会话表信息接口")
    public Result<Conversation> create(String receiverId, String productId) {
        OnlineUser onlineUser = SpringUtil.getOnlineUser();
        return new Result<>(conversationService.create(receiverId, productId, onlineUser), "创建成功");
    }

    /**
     * 修改会话表信息
     *
     * @param conversation 会话表实体信息
     * @return 修改后的会话表信息
     */
    @PatchMapping("/{id}")
    @Auth(value = "order-second-hand-transaction.conversation.update", name = "修改会话表", description = "修改会话表信息接口")
    public Result<Conversation> update(@Validated Conversation conversation, @PathVariable("id") String id) {
        OnlineUser onlineUser = SpringUtil.getOnlineUser();
        conversation.setId(id);
        return new Result<>(conversationService.update(conversation, onlineUser), "修改成功");
    }

    /**
     * 保存会话表信息
     *
     * @param conversation 会话表实体信息
     * @return 保存后的会话表信息
     */
    @PostMapping("/save")
    @Auth(value = "order-second-hand-transaction.conversation.save", name = "保存会话表", description = "保存会话表信息接口")
    public Result<Conversation> save(@Validated Conversation conversation) {
        OnlineUser onlineUser = SpringUtil.getOnlineUser();
        return new Result<>(conversationService.save(conversation, onlineUser), "保存成功");
    }

    /**
     * 根据会话表id删除会话表信息
     *
     * @param id id
     */
    @DeleteMapping("/{id}")
    @Auth(value = "order-second-hand-transaction.conversation.delete", name = "删除会话表", description = "删除会话表信息接口")
    public Result<String> delete(@PathVariable("id") String id) {
        OnlineUser onlineUser = SpringUtil.getOnlineUser();
        conversationService.delete(id, onlineUser);
        return Result.ofSuccess("删除成功");
    }
}
